﻿namespace TaskTest
{
    internal class Program
    {

        //1、使用并行的前提。数据运算多CPU 使用并行 列如：算法运行，人工智能算法  
        //2、使用并发的前提。文件操作多I/O 使用并发。列如文件上传

        //线程池最小数量 系统内核算出来的
        //线程池最大线程数量  操作系统最大能承受
        //线程池的可用线程数量  操作系统最大能承受
        static void Main(string[] args)
        {
            //Console.WriteLine("Hello, World!");
            //Thread thread = new Thread(new
            //    Model.Model.ModelTask().ModelRun);
            //thread.Start();
            //Console.WriteLine("1");
            //ThreadPool.GetMinThreads(out int a,out int b);
            //ThreadPool.GetMaxThreads(out int c, out int d);
            //Console.WriteLine( $"{a},{c}");
            //var ww = Task.Run(() => xxx("xx")).Result;


            //并行
            //Parallel 将线程调度到不同的Cpu分别执行 
            //Task.Run(() => { Console.WriteLine("xx"); });
            //Thread.Sleep(3000);

            //List<Action> actions = new List<Action>();
            //actions.Add(() => { xxx("xx"); });
            //actions.Add(() => { xxx("xx"); });
            //actions.Add(() => { xxx("xx"); });
            //actions.Add(() => { xxx("xx"); });
            //actions.Add(() => { xxx("xx"); });
            //actions.Add(() => { xxx("xx"); });
            //actions.Add(() => { xxx("xx"); });
            //actions.Add(() => { xxx("xx"); });
            //ParallelOptions parallelOptions = new ParallelOptions();
            //parallelOptions.MaxDegreeOfParallelism = 4; //指定8个cpu核心来执行  默认是随机，操作系统分配的。根据CPU的空闲状态分配的
            //Parallel.ForEach(actions, parallelOptions, xx => { xx.Invoke(); });
            ////Paraller 封装了Task Task封装了ThreadPool ThreadPool封装了 Thread 
            


            //List<Action> actions2 = new List<Action>();
            //actions2.Add(() => { xxx("xx"); });
            //actions2.Add(() => { xxx("xx"); });
            //actions2.Add(() => { xxx("xx"); });
            //actions2.Add(() => { xxx("xx"); });
            //ParallelOptions parallelOptions2 = new ParallelOptions();
            //parallelOptions.MaxDegreeOfParallelism = 4; //指定8个cpu核心来执行  默认是随机，操作系统分配的。根据CPU的空闲状态分配的
            //Parallel.ForEach(actions2, parallelOptions2, xx => { xx.Invoke(); });




            //并发 充分使用资源 Task.WaitAll();
            //List<Task> tasks = new List<Task>();
            //tasks.Add(xxx("xx"));
            //tasks.Add(xxx("xx"));
            //tasks.Add(xxx("xx"));
            //Task.WhenAll(tasks);
            //Console.WriteLine("xxwwwwwww");


            //Task.Run(() => { xxx(""); });
            //Task.Run(() => { xxx(""); });
            //Task.Run(() => { xxx(""); });

            //CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            //var a = cancellationTokenSource.Token;
            //Task task = Task.Run(() => { xxx("",a); }, a);

            //try
            //{
            //    Task.Delay(3200).Wait();
            //    cancellationTokenSource.Cancel();
            //    task.Wait();
            //}
            //catch (Exception ex)
            //{

            //}
            //Console.WriteLine();

            Thread thread = new Thread(www);
            // 将该线程设置为后台线程  
            thread.IsBackground = true;
            thread.Start();
            Console.WriteLine("xxxx");
        }

        public static void xxx(string xx, CancellationToken cancellationToken)
        {
            for (var i = 0; i < 5; i++)
            {
                Console.WriteLine(i);
                Thread.Sleep(1000);
                if (cancellationToken.IsCancellationRequested)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                }
            }
            //Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
        }

        public static void www()
        {
            for (var i = 0; i < 10; i++)
            {
                Console.WriteLine(i);
                Thread.Sleep(1000);
            }
            //Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
        }
    }
}
